package com.wwwarehouse.usercenter.fragment.business_relationship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.FilterMultipleDoubleAdapter;
import com.wwwarehouse.usercenter.adapter.FilterMultipleTrebleAdapter;
import com.wwwarehouse.usercenter.adapter.SelectPermissionPagerAdapter;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileCheckBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileListBean;
import com.wwwarehouse.usercenter.bean.business_relationship.GetSalesmansAndTagsByBuIdBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.eventbus_event.DrawerMultipleResetDoubleEvent;
import com.wwwarehouse.usercenter.eventbus_event.DrawerMultipleResetTrebleEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Jackie on 2017/8/25.
 * 项目中的抽屉布局(多选)
 */

public class DeskDrawerMultipleTrebleFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private TextView mTitleTxt;
    private GridView mGridView;
    private TextView mTitleTxt2;
    private GridView mGridView2;
    private TextView mTitleTxt3;
    private GridView mGridView3;
    private Button mRestBtn;
    private Button mCompleteBtn;

    private String mTitle;
    private List<BRCustomerFileCheckBean.ScreenBean> mFilterList;
    private GetSalesmansAndTagsByBuIdBean getSalesmansAndTagsByBuIdBean;
    private List<GetSalesmansAndTagsByBuIdBean.ScreeningTagsBean> screeningTagsBeanList;
    private List<GetSalesmansAndTagsByBuIdBean.ScreeningSalesMansBean> screeningSalesMansBeenList;

    private FilterMultipleTrebleAdapter mAdapter;
    private FilterMultipleTrebleAdapter mAdapter2;
    private FilterMultipleTrebleAdapter mAdapter3;
    private int mColumns;

    private ArrayList<String> mSelectedPositionSet;
    private ArrayList<String> mSelectedPositionSet2;
    private ArrayList<String> mSelectedPositionSet3;

    private static final String KEY_TITLE = "KEY_TITLE";
    private static final String KEY_LIST = "KEY_LIST_1";
    private static final String KEY_COLUMNS = "KEY_COLUMNS";


    public static final DeskDrawerMultipleTrebleFragment newInstance(String title, List<BRCustomerFileCheckBean.ScreenBean> filterList, int columns) {
        DeskDrawerMultipleTrebleFragment deskDrawerMultipleFragment = new DeskDrawerMultipleTrebleFragment();

        Bundle bundle = new Bundle();
        bundle.putString(KEY_TITLE, title);
        bundle.putSerializable(KEY_LIST, (Serializable) filterList);
        bundle.putInt(KEY_COLUMNS, columns);

        deskDrawerMultipleFragment.setArguments(bundle);

        return deskDrawerMultipleFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_desk_drawer_multiple_double, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
        initData();
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(KEY_TITLE);
            mFilterList = (List<BRCustomerFileCheckBean.ScreenBean>) bundle.getSerializable(KEY_LIST);
            mColumns = bundle.getInt(KEY_COLUMNS);
            mGridView2.setNumColumns(2);
            mGridView3.setNumColumns(2);
            mGridView.setNumColumns(mColumns);

            mTitleTxt.setText(mTitle);

            mAdapter = new FilterMultipleTrebleAdapter(mActivity, mFilterList, null, null);
            mAdapter.setOnItemListener(new OnItemListener() {
                @Override
                public void onItemListener(String buId) {
                    if (buId != null) {
                        getSalesmansAndTagsByBuId(buId);
                    } else {
                        if (screeningTagsBeanList != null)
                            screeningTagsBeanList.clear();
                        if (screeningSalesMansBeenList != null)
                            screeningSalesMansBeenList.clear();
                        mAdapter2.notifyDataSetChanged();
                        mAdapter3.notifyDataSetChanged();
                    }
                }
            });
            mGridView.setAdapter(mAdapter);
        }
        mSelectedPositionSet = new ArrayList<>();
        mSelectedPositionSet2 = new ArrayList<>();
        mSelectedPositionSet3 = new ArrayList<>();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mTitleTxt = findView(mRootView, R.id.tv_title);
        mGridView = findView(mRootView, R.id.gv_content);
        mTitleTxt2 = findView(mRootView, R.id.tv_title2);
        mGridView2 = findView(mRootView, R.id.gv_content2);
        mTitleTxt3 = findView(mRootView, R.id.tv_title3);
        mGridView3 = findView(mRootView, R.id.gv_content3);
        mRestBtn = findView(mRootView, R.id.btn_reset);
        mCompleteBtn = findView(mRootView, R.id.btn_complete);

    }

    private void initEvent() {
        mRestBtn.setOnClickListener(this);
        mCompleteBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_reset) {
            mSelectedPositionSet.clear();
            mSelectedPositionSet2.clear();
            mSelectedPositionSet3.clear();

            for (BRCustomerFileCheckBean.ScreenBean screenBean : mFilterList) {
                screenBean.setSelected(false);
            }

//            for(GetSalesmansAndTagsByBuIdBean.ScreeningSalesMansBean screeningSalesMansBean : screeningSalesMansBeenList){
//                screeningSalesMansBean.setSelected(false);
//            }
//            for(GetSalesmansAndTagsByBuIdBean.ScreeningTagsBean screeningTagsBean : screeningTagsBeanList){
//                screeningTagsBean.setSelected(false);
//            }
            screeningSalesMansBeenList.clear();
            screeningTagsBeanList.clear();

            mAdapter.notifyDataSetChanged();
            mAdapter2.notifyDataSetChanged();
            mAdapter3.notifyDataSetChanged();

            EventBus.getDefault().post(new DrawerMultipleResetTrebleEvent(mSelectedPositionSet, mSelectedPositionSet2, mSelectedPositionSet3));

        } else if (id == R.id.btn_complete) {
            mSelectedPositionSet.clear();
            mSelectedPositionSet2.clear();
            mSelectedPositionSet3.clear();

            for (int i = 0; i < mFilterList.size(); i++) {
                if (mFilterList.get(i).isSelected()) {
                    mSelectedPositionSet.add(mFilterList.get(i).getBuId());
                }
            }
            if (getSalesmansAndTagsByBuIdBean != null) {
                for (int i = 0; i < getSalesmansAndTagsByBuIdBean.getScreeningTags().size(); i++) {
                    if (getSalesmansAndTagsByBuIdBean.getScreeningTags().get(i).isSelected()) {
                        mSelectedPositionSet2.add(getSalesmansAndTagsByBuIdBean.getScreeningTags().get(i).getTagId());
                    }
                }
                for (int i = 0; i < getSalesmansAndTagsByBuIdBean.getScreeningSalesMans().size(); i++) {
                    if (getSalesmansAndTagsByBuIdBean.getScreeningSalesMans().get(i).isSelected()) {
                        mSelectedPositionSet3.add(getSalesmansAndTagsByBuIdBean.getScreeningSalesMans().get(i).getSalesManId());
                    }
                }
            }


            EventBus.getDefault().post(new DrawerMultipleResetTrebleEvent(mSelectedPositionSet, mSelectedPositionSet2, mSelectedPositionSet3));

        } else {
        }
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    /**
     * 通过业务单位返回对应标签及业务员
     */
    private void getSalesmansAndTagsByBuId(String buId) {
        Map<String, Object> map = new HashMap<>();
        map.put("buId", buId);//业务单位所有id
        NoHttpUtils.httpPost(UserCenterConstant.GET_SALESMANS_AND_TAGS_BY_BUID, map, onResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        getSalesmansAndTagsByBuIdBean = JSON.parseObject(commonClass.getData().toString(), GetSalesmansAndTagsByBuIdBean.class);
                        screeningTagsBeanList = getSalesmansAndTagsByBuIdBean.getScreeningTags();
                        screeningSalesMansBeenList = getSalesmansAndTagsByBuIdBean.getScreeningSalesMans();
                        mTitleTxt2.setText(getString(R.string.user_br_tags));
                        mTitleTxt3.setText(getString(R.string.user_br_salesman));

                        mAdapter2 = new FilterMultipleTrebleAdapter(mActivity, null, screeningTagsBeanList, null);
                        mGridView2.setAdapter(mAdapter2);
                        mAdapter3 = new FilterMultipleTrebleAdapter(mActivity, null, null, screeningSalesMansBeenList);
                        mGridView3.setAdapter(mAdapter3);

                    } else {
                        ToastUtils.showToast(commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };

    public interface OnItemListener {
        void onItemListener(String buId);
    }

}
